<!--
 * @Description: 属性项弹窗
 * @Author: 龙春雨
 * @Date: 2023-04-12 11:22:31
-->
<template>
  <el-dialog
    title="关联属性项"
    :visible.sync="_visible"
    width="1000px"
    append-to-body
  >
    <avue-crud
      v-if="visible"
      ref="crud"
      v-model="form"
      :data="tableData"
      :page.sync="page"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template #menuLeft>
        <el-button
          type="primary"
          size="default"
          @click="handleSave"
        >新增</el-button>
      </template>
      <template #menu="{row}">
        <el-button
          type="text"
          size="default"
          @click="handleUpdate(row)"
        >编辑</el-button>
        <el-button
          type="text"
          size="default"
          @click="handleDelete(row)"
        >删除</el-button>
      </template>
    </avue-crud>
    <add-dialog
      :visible.sync="addVisible"
      :classify-code="classifyCode1"
      :attribute-name="attributeName1"
      :attribute-code="attributeCode"
      :attribute-code-list="attributeCodeList"
      :form-type="formType"
      :form-data="formData"
      @getRefresh="getList"
    />
  </el-dialog>
</template>

<script>
import crudMixins from '@/mixins/crud';
import { tableOption } from '@/const/crud/pet/classifyAttributeItem';
import addDialog from './addDialog.vue';
import {
  getBindPetAttributePage,
  delBindPetAttribute,
  getAttributes,
  getBindPetAttributeList,
} from '@/api/core/bindAttribute';
export default {
  name: 'bindAttributeDialog',
  components: {
    addDialog,
  },
  mixins: [crudMixins],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    classifyCode: {
      // 品种code
      type: String,
    },
    attributeName: {
      type: String,
    },
    attributeCode: {
      // 属性码
      type: String,
    },
  },
  data() {
    return {
      form: {},
      tableOption,
      addVisible: false,
      attributeCodeList: [],
      formType: '',
      formData: {},
    };
  },
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      },
    },
    classifyCode1() {
      return this.classifyCode;
    },
    attributeName1() {
      return this.attributeName;
    },
  },
  methods: {
    // 获取列表
    async getList() {
      try {
        this.tableLoading = true;
        const { data } = await getBindPetAttributePage({
          classifyCode: this.classifyCode,
          attributeCode: this.attributeCode,
          size: this.page.pageSize,
          current: this.page.currentPage,
          ...this.searchForm,
        });
        this.tableData = data.data.records;
        this.page.total = data.data.total;
        this.tableLoading = false;
      } catch (error) {
        this.tableLoading = false;
        throw new Error(error);
      }
    },
    // 编辑
    handleUpdate(row) {
      this.addVisible = true;
      this.formType = 'edit';
      this.formData = row;
      this.getAttributeList();
    },
    // 保存
    handleSave(row) {
      this.addVisible = true;
      this.formType = 'add';
      this.getAttributeList();
    },
    // 删除
    handleDelete: function (row, index) {
      this.$confirm('是否确认删除该宠物属性项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delBindPetAttribute(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    beforeOpen(done) {
      this.form.name = this.typeName;
      this.form.classifyCode = this.classifyCode;
      done();
    },
    async getAttributeList() {
      const resList = await getBindPetAttributeList(
        this.classifyCode,
        this.attributeCode
      );
      const res = await getAttributes(this.attributeCode);
      if (this.formType === 'add') {
        for (let i = 0; i < resList.data.data.length; i++) {
          for (let j = 0; j < res.data.data.length; j++) {
            if (
              resList.data.data[i].attributeCode ===
              res.data.data[j].attributeCode
            ) {
              res.data.data.splice(j, 1);
            }
          }
        }
      }
      this.attributeCodeList = res.data.data;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
